﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using Advisor.Entities;
using System.Collections;

namespace Advisor.Entities
{
    public class RiskDependenceDAO
    {
        private Users m_User;
        public RiskDependenceDAO(Users user)
        {
            this.m_User = user;
        }

        /// <summary>
        /// Retorna todas os registros da tabela RiskDependence. 
        /// </summary>
        /// <param name="column"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        internal IList GetRiskDependence(string column, int maximumRows, int startRowIndex)
        {
            ISession session = NHibernateSession.GetSession();
            string sql = "FROM RISKDEPENDENCE as riskDependence";

            //Ordenação
            if (!String.IsNullOrEmpty(column))
            {
                sql += " ORDER BY riskDependence." + column;
            }
            IQuery query = session.CreateQuery(sql);

            //Paginando
            query.SetFirstResult(startRowIndex);
            query.SetMaxResults(maximumRows);

            return query.List();
        }

        /// <summary>
        ///  Método para inserir um RiskDependence no banco. 
        /// </summary>
        /// <param name="history"></param>
        internal void InsertRiskDependence(IdRiskdependence riskDependence)
        {
            ISession session = NHibernateSession.GetSession();
            session.Save(riskDependence);
            session.Close();
        }

        /// <summary>
        /// Retorna uma instância da tabela RiskDependence passando o id;
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       internal IdRiskdependence GetRiskDependence(int id)
        {
            ISession session = NHibernateSession.GetSession();
            return (IdRiskdependence)session.Get(typeof(IdRiskdependence), id);
        }

        /// <summary>
        /// Remove um RiskDependence do banco
        /// </summary>
        /// <param name="history"></param>
        public void DeleteRiskDependence(IdRiskdependence riskDependence)
        {
            ISession sessions = NHibernateSession.GetSession();
            sessions.Delete(riskDependence);
            sessions.Close();
        }

        /// <summary>
        /// Atualiza um RiskDependence no banco. 
        /// </summary>
        /// <param name="history"></param>
        public void UpdateHistory(IdRiskdependence riskDependence)
        {
            ISession session = NHibernateSession.GetSession();
            session.Update(riskDependence);
            //Comando que ativa as alterações no banco de dados. 
            session.Flush();
            session.Close();
        }

    }
}
